/*
 * ProgressTableCellRenderer.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui.elements;

import java.awt.Color;
import java.awt.Component;
import java.awt.Rectangle;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.border.Border;
import org.tiberiumlabs.lailaps.gui.elements.ui.ProgressBarUI;
import org.tiberiumlabs.lailaps.list.DownloadItem;
import org.tiberiumlabs.lailaps.list.StatusConstants;

/**
 *
 * @author <a href="mailto:ss.paranoid@gmail.com">Paranoid</a>
 */
public class ProgressTableCellRenderer extends JProgressBar implements javax.swing.table.TableCellRenderer {

    private static final Color ALTERNATIVE_BACKGROUND = TableCellRenderer.ALTERNATIVE_BACKGROUND;
    //private static final Color DEFAULT_FOREGROUND = UIManager.getColor("ProgressBar.foreground");// dark-gray
    
    private static final String ZERO = "0%";
    private static final String FULL = "100%";

    public ProgressTableCellRenderer() {
        super(0, 100);
        setStringPainted(true);
        setUI(new ProgressBarUI());
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        setValue((DownloadItem)value);

        setFont(table.getFont());
        if (isSelected) {
            setValue(100);
            setBackground(table.getSelectionBackground());
            setBorder(null);
        }
        else {
            if (row % 2 != 0) {
                setBackground(ALTERNATIVE_BACKGROUND);
            }
            else {
                setBackground(table.getBackground());
            }
            if (hasFocus) {
                Border border = null;
                if (isSelected) {
                    border = TableCellRenderer.tableFocusSelectedCellHighlightBorder;
                }
                else {
                    border = TableCellRenderer.tableFocusCellHighlightBorder;
                }
                setBorder(border);
            }
            else {
                setBorder(TableCellRenderer.noFocusBorder);
            }
        }


        return this;
    }

    protected void setValue(DownloadItem item) {
        if (item.getCompletedSize() <= 0) {
            setString(ZERO);
            setToolTipText(null);
            setValue(0);
            return;
        }
        if (item.getStatus() == StatusConstants.COMPLETED) {
            setString(FULL);
            setToolTipText(FULL);
            setValue(100);
            return;
        }
        int percent = getPercentCompleted(item.getCompletedSize(), item.getFullSize());
        if (percent != getValue()) {
            setValue(percent);
            String string = percent + "%";
            setString(string);
            setToolTipText(string);
        }
    }

    protected int getPercentCompleted(long currentValue, long maximumValue) {
        if (currentValue == maximumValue || currentValue >= maximumValue) {
            return 100;
        }
        double part = (double)currentValue/(double)maximumValue;
        return (int)(part * 100d);
    }

    // <editor-fold defaultstate="collapsed" desc=" do same as DefaultTableCellRenderer ">

    @Override
    public void invalidate() {
    }

    @Override
    public void validate() {
    }

    @Override
    public void revalidate() {
    }

    @Override
    public void repaint() {
    }

    @Override
    public void repaint(Rectangle r) {
    }

    @Override
    public void repaint(long tm, int x, int y, int width, int height) {
    }

    // </editor-fold>

}
